<?php

namespace Home\Controller;

class ShopAddressController extends CommonController
{
    //地区表数据
    public function area_data()
    {
        $erae_mode = D('Erea');
        $area_data = $erae_mode->area_data();
        json_data($area_data);
    }

    //添加收货地址和修改地址
    public function address()
    {
        $member_data = session('member_data');

        $address_model = D('Address');
        $member_id = $member_data['accountId'];
        $address_id = I('address_id');

        if (empty($member_id)) {
            json_error('参数错误');
        }

        $data = array(
            'name' => $_POST['name'],
            'telephone' => $_POST['telephone'],
            'area_name' => $_POST['area_name'],
            'address' => $_POST['address'],
            'province_id' => $_POST['province_id'],
            'city_id' => $_POST['city_id'],
            'country_id' => $_POST['country_id'],
            'default_address' => $_POST['default_address'],
        );

        if (!empty($address_id)) {
            //编辑收货地址
            $address_data = $address_model->getAddressInfo(array('address_id' => $address_id, 'member_id' => $member_id));
            if (empty($address_data)) {
                json_error('数据不存在');
            }
            if (!empty($data['default_address'])) {
                $default_w = array(
                    'member_id' => $member_id,
                    'default_address' => 1,
                );
                $default_d = array(
                    'default_address' => 0
                );
                $address_model->editAddress($default_w, $default_d);
            }
            $result = $address_model->editAddress(array('address_id' => $address_id, 'member_id' => $member_id), $data);

        } else {
            //添加收货地址
            //统计地址数量
            $count = $address_model->addressCount(array('member_id' => $member_id));
            if ($count > '10') {
                json_error('最多只能添加10条收货地址');
            }
            $data['member_id'] = $member_id;
            $result = $address_model->addAddress($data);
        }

        if ($result) {
            json_data('保存成功');
        } else {
            json_error('保存失败');
        }


    }

    //收货地址列表
    public function address_list()
    {
        $member_data = session('member_data');

        $address_model = D('Address');
        $member_id = $member_data['accountId'];
        if (empty($member_id)) {
            json_error('参数错误');
        }
        $where = array(
            'member_id' => $member_id,
        );
        //总数、分页
        $count = $address_model->addressCount($where);

        //接收post转换get
        //$_GET['page'] = $_POST['page'];

        //分页数
        //$page = $_POST['page'];

        //一页显示的条数
        //$num = 10;

        //返回是否有下页数据
        //$load = api_page($page, $num, $count);
        $order = 'address_id desc';
        $address_list = $address_model->getAddressList($where, '', $order, 10);
        json_data(array('count'=>$count,'data_list' => $address_list));
    }


    //设置默认地址
    public function set_defaultAddress()
    {

        $member_data = session('member_data');

        $address_model = D('Address');
        $member_id = $member_data['accountId'];
        $address_id = I('address_id');
        $default_address = I('default_address');

        if (empty($member_id) || empty($address_id)) {
            json_error('参数错误');
        }

        //编辑收货地址
        $address_data = $address_model->getAddressInfo(array('address_id' => $address_id, 'member_id' => $member_id));

        if (empty($address_data)) {
            json_error('数据不存在');
        }
        //修改等于1的数据修改为0
        $default_w = array(
            'member_id' => $member_id,
            'default_address' => 1,
        );
        $default_d = array(
            'default_address' => 0
        );
        $address_model->editAddress($default_w, $default_d);

        //设置指定id的数据为1
        $data = array(
            'default_address' => $default_address,
        );
        $result = $address_model->editAddress(array('address_id' => $address_id, 'member_id' => $member_id), $data);

        if ($result) {
            json_data('设置成功');
        } else {
            json_error('设置失败');
        }

    }


    //删除地址
    public function delAddress()
    {

        $member_data = session('member_data');

        $address_model = D('Address');
        $member_id = $member_data['accountId'];
        $address_id = I('address_id');


        if (empty($member_id) || empty($address_id)) {
            json_error('参数错误');
        }

        //编辑收货地址
        $address_data = $address_model->getAddressInfo(array('address_id' => $address_id, 'member_id' => $member_id));

        if (empty($address_data)) {
            json_error('数据不存在');
        }

        if ($address_data['default_address'] == '1') {
            json_error('不能删除默认地址');
        }

        //删除
        $where = array(
            'address_id' => $address_id,
            'member_id' => $member_id
        );
        $result = $address_model->delAddress($where);

        if ($result) {
            json_data('删除成功');
        } else {
            json_error('删除失败');
        }

    }

}